﻿package igz.fleaxy.ui.text {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.filters.BitmapFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import igz.fleaxy.util.ImageUtil;
	
	/**
	 * ...
	 * @author Intelygenz | www.intelygenz.com
	 */
	public class BitmapLabelUi extends Bitmap{

		[ArrayElementType("flash.filters:BitmapFilter")]
		protected var _Filters:Array;
		override public function get filters () : Array {
			return _Filters;
		}
		override public function set filters (value:Array) : void {
			_Filters = value;
			var filter:BitmapFilter;
			for each ( filter in _Filters ) {
				bitmapData.applyFilter(bitmapData, bitmapData.rect, new Point(), filter);
			}
		}

		protected var _Text:String;
		public function set $Text(p_htmlText:String) : void {
			_Text = p_htmlText;
			_RefreshBitmapData();
		}

		protected var _Style:String;
		public function set $Style(p_style:String) : void {
			_Style = p_style;
			_RefreshBitmapData();
		}
		protected var _Settings:Object;

		protected var _FixWidth:Number;
		public function set $FixWidth(p_fixWidth:Number) : void {
			_FixWidth = p_fixWidth;
			_RefreshBitmapData();
		}

		protected var _MaxLines:Number;
		public function set $MaxLines(p_maxLines:Number) : void {
			_MaxLines = p_maxLines;
			_RefreshBitmapData();
		}

		/**
		 * 
		 * @param	p_htmlText
		 * @param	p_style
		 * @param	p_settings _Settings = {
								"fixWidth" : ""
								"maxLines" : 4
								};
		 */
		public function BitmapLabelUi(p_htmlText:String = "", p_style:String = "", p_settings:Object = null) {
			smoothing = true;
			_Text = p_htmlText;
			_Style = p_style;
			_Settings = p_settings;
			_FixWidth = (_Settings!=null && _Settings["fixWidth"] != null) ? _Settings["fixWidth"] : -1;
			_MaxLines = (_Settings!=null && _Settings["maxLines"] != null) ? _Settings["maxLines"] : -1;
			$Text = p_htmlText;
		}

		protected function _RefreshBitmapData() : void {
			var labelUi:LabelUi = new LabelUi(_Text, _Style, _Settings);
			if ( _FixWidth!=-1 ) labelUi.$SetFixWidth(_FixWidth);
			if ( _MaxLines != -1 ) labelUi.$SetMaxLines(_MaxLines);
			bitmapData = new BitmapData( labelUi.width, labelUi.height, true, 0x00000000 );
			bitmapData.draw(labelUi);
			labelUi = null;
		}
	}

}